home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / input-sc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-03  |  12.1 KB  |  417 lines

  1. /* input_scrub.c - Break up input buffers into whole numbers of lines.
  2.    Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  19.  
  20. #include <errno.h>        /* Need this to make errno declaration right */
  21. #include "as.h"
  22. #include "input-file.h"
  23.  
  24. /*
  25.  * O/S independent module to supply buffers of sanitised source code
  26.  * to rest of assembler. We get sanitized input data of arbitrary length.
  27.  * We break these buffers on line boundaries, recombine pieces that
  28.  * were broken across buffers, and return a buffer of full lines to
  29.  * the caller.
  30.  * The last partial line begins the next buffer we build and return to caller.
  31.  * The buffer returned to caller is preceeded by BEFORE_STRING and followed
  32.  * by AFTER_STRING, as sentinels. The last character before AFTER_STRING
  33.  * is a newline.
  34.  * Also looks after line numbers, for e.g. error messages.
  35.  */
  36.  
  37. /*
  38.  * We don't care how filthy our buffers are, but our callers assume
  39.  * that the following sanitation has already been done.
  40.  *
  41.  * No comments, reduce a comment to a space.
  42.  * Reduce a tab to a space unless it is 1st char of line.
  43.  * All multiple tabs and spaces collapsed into 1 char. Tab only
  44.  *   legal if 1st char of line.
  45.  * # line file statements converted to .line x;.file y; statements.
  46.  * Escaped newlines at end of line: remove them but add as many newlines
  47.  *   to end of statement as you removed in the middle, to synch line numbers.
  48.  */
  49.  
  50. #define BEFORE_STRING ("\n")
  51. #define AFTER_STRING ("\0")    /* memcpy of 0 chars might choke. */
  52. #define BEFORE_SIZE (1)
  53. #define AFTER_SIZE  (1)
  54.  
  55. static char *buffer_start;    /*->1st char of full buffer area. */
  56. static char *partial_where;    /*->after last full line in buffer. */
  57. static int partial_size;    /* >=0. Number of chars in partial line in buffer. */
  58. static char save_source[AFTER_SIZE];
  59. /* Because we need AFTER_STRING just after last */
  60. /* full line, it clobbers 1st part of partial */
  61. /* line. So we preserve 1st part of partial */
  62. /* line here. */
  63. static unsigned int buffer_length;    /* What is the largest size buffer that */
  64. /* input_file_give_next_buffer() could */
  65. /* return to us? */
  66.  
  67. /* Saved information about the file that .include'd this one.  When we hit EOF,
  68.    we automatically pop to that file. */
  69.  
  70. static char *next_saved_file;
  71.  
  72. /* We can have more than one source file open at once, though the info for all
  73.    but the latest one are saved off in a struct input_save.  These files remain
  74.    open, so we are limited by the number of open files allowed by the
  75.    underlying OS. We may also sequentially read more than one source file in an
  76.    assembly. */
  77.  
  78. /* We must track the physical file and line number for error messages. We also
  79.    track a "logical" file and line number corresponding to (C?)  compiler
  80.    source line numbers.  Whenever we open a file we must fill in
  81.    physical_input_file. So if it is NULL we have not opened any files yet. */
  82.  
  83. static char *physical_input_file;
  84. static char *logical_input_file;
  85.  
  86. typedef unsigned int line_numberT;    /* 1-origin line number in a source file. */
  87. /* A line ends in '\n' or eof. */
  88.  
  89. static line_numberT physical_input_line;
  90. static int logical_input_line;
  91.  
  92. /* Struct used to save the state of the input handler during include files */
  93. struct input_save
  94.   {
  95.     char *buffer_start;
  96.     char *partial_where;
  97.     int partial_size;
  98.     char save_source[AFTER_SIZE];
  99.     unsigned int buffer_length;
  100.     char *physical_input_file;
  101.     char *logical_input_file;
  102.     line_numberT physical_input_line;
  103.     int logical_input_line;
  104.     char *next_saved_file;    /* Chain of input_saves */
  105.     char *input_file_save;    /* Saved state of input routines */
  106.     char *saved_position;    /* Caller's saved position in buf */
  107.   };
  108.  
  109. static char *input_scrub_push PARAMS ((char *saved_position));
  110. static char *input_scrub_pop PARAMS ((char *arg));
  111. static void as_1_char PARAMS ((unsigned int c, FILE * stream));
  112.  
  113. /* Push the state of input reading and scrubbing so that we can #include.
  114.    The return value is a 'void *' (fudged for old compilers) to a save
  115.    area, which can be restored by passing it to input_scrub_pop(). */
  116. static char *
  117. input_scrub_push (saved_position)
  118.      char *saved_position;
  119. {
  120.   register struct input_save *saved;
  121.  
  122.   saved = (struct input_save *) xmalloc (sizeof *saved);
  123.  
  124.   saved->saved_position = saved_position;
  125.   saved->buffer_start = buffer_start;
  126.   saved->partial_where = partial_where;
  127.   saved->partial_size = partial_size;
  128.   saved->buffer_length = buffer_length;
  129.   saved->physical_input_file = physical_input_file;
  130.   saved->logical_input_file = logical_input_file;
  131.   saved->physical_input_line = physical_input_line;
  132.   saved->logical_input_line = logical_input_line;
  133.   memcpy (saved->save_source, save_source, sizeof (save_source));
  134.   saved->next_saved_file = next_saved_file;
  135.   saved->input_file_save = input_file_push ();
  136.  
  137.   input_file_begin ();        /* Reinitialize! */
  138.   logical_input_line = -1;
  139.   logical_input_file = (char *) NULL;
  140.   buffer_length = input_file_buffer_size ();
  141.  
  142.   buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
  143.   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
  144.  
  145.   return ((char *) saved);
  146. }                /* input_scrub_push() */
  147.  
  148. static char *
  149. input_scrub_pop (arg)
  150.      char *arg;
  151. {
  152.   register struct input_save *saved;
  153.   char *saved_position;
  154.  
  155.   input_scrub_end ();        /* Finish off old buffer */
  156.  
  157.   saved = (struct input_save *) arg;
  158.  
  159.   input_file_pop (saved->input_file_save);
  160.   saved_position = saved->saved_position;
  161.   buffer_start = saved->buffer_start;
  162.   buffer_length = saved->buffer_length;
  163.   physical_input_file = saved->physical_input_file;
  164.   logical_input_file = saved->logical_input_file;
  165.   physical_input_line = saved->physical_input_line;
  166.   logical_input_line = saved->logical_input_line;
  167.   partial_where = saved->partial_where;
  168.   partial_size = saved->partial_size;
  169.   next_saved_file = saved->next_saved_file;
  170.   memcpy (save_source, saved->save_source, sizeof (save_source));
  171.  
  172.   free (arg);
  173.   return saved_position;
  174. }
  175.  
  176.  
  177. void
  178. input_scrub_begin ()
  179. {
  180.   know (strlen (BEFORE_STRING) == BEFORE_SIZE);
  181.   know (strlen (AFTER_STRING) == AFTER_SIZE || (AFTER_STRING[0] == '\0' && AFTER_SIZE == 1));
  182.  
  183.   input_file_begin ();
  184.  
  185.   buffer_length = input_file_buffer_size ();
  186.  
  187.   buffer_start = xmalloc ((BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
  188.   memcpy (buffer_start, BEFORE_STRING, (int) BEFORE_SIZE);
  189.  
  190.   /* Line number things. */
  191.   logical_input_line = -1;
  192.   logical_input_file = (char *) NULL;
  193.   physical_input_file = NULL;    /* No file read yet. */
  194.   next_saved_file = NULL;    /* At EOF, don't pop to any other file */
  195.   do_scrub_begin ();
  196. }
  197.  
  198. void
  199. input_scrub_end ()
  200. {
  201.   if (buffer_start)
  202.     {
  203.       free (buffer_start);
  204.       buffer_start = 0;
  205.       input_file_end ();
  206.     }
  207. }
  208.  
  209. /* Start reading input from a new file. */
  210.  
  211. char *                /* Return start of caller's part of buffer. */
  212. input_scrub_new_file (filename)
  213.      char *filename;
  214. {
  215.   input_file_open (filename, !flag_no_comments);
  216.   physical_input_file = filename[0] ? filename : "{standard input}";
  217.   physical_input_line = 0;
  218.  
  219.   partial_size = 0;
  220.   return (buffer_start + BEFORE_SIZE);
  221. }
  222.  
  223.  
  224. /* Include a file from the current file.  Save our state, cause it to
  225.    be restored on EOF, and begin handling a new file.  Same result as
  226.    input_scrub_new_file. */
  227.  
  228. char *
  229. input_scrub_include_file (filename, position)
  230.      char *filename;
  231.      char *position;
  232. {
  233.   next_saved_file = input_scrub_push (position);
  234.   return input_scrub_new_file (filename);
  235. }
  236.  
  237. void
  238. input_scrub_close ()
  239. {
  240.   input_file_close ();
  241. }
  242.  
  243. char *
  244. input_scrub_next_buffer (bufp)
  245.      char **bufp;
  246. {
  247.   register char *limit;        /*->just after last char of buffer. */
  248.  
  249.   *bufp = buffer_start + BEFORE_SIZE;
  250.  
  251.   if (partial_size)
  252.     {
  253.       memcpy (buffer_start + BEFORE_SIZE, partial_where,
  254.           (unsigned int) partial_size);
  255.       memcpy (buffer_start + BEFORE_SIZE, save_source, AFTER_SIZE);
  256.     }
  257.   limit = input_file_give_next_buffer (buffer_start + BEFORE_SIZE + partial_size);
  258.   if (limit)
  259.     {
  260.       register char *p;        /* Find last newline. */
  261.  
  262.       for (p = limit; *--p != '\n';);;
  263.       ++p;
  264.       if (p <= buffer_start + BEFORE_SIZE)
  265.     {
  266.       as_fatal ("Source line too long. Please change file %s then rebuild assembler.", __FILE__);
  267.     }
  268.       partial_where = p;
  269.       partial_size = limit - p;
  270.       memcpy (save_source, partial_where, (int) AFTER_SIZE);
  271.       memcpy (partial_where, AFTER_STRING, (int) AFTER_SIZE);
  272.     }
  273.   else
  274.     {
  275.       partial_where = 0;
  276.       if (partial_size > 0)
  277.     {
  278.       as_warn ("Partial line at end of file ignored");
  279.     }
  280.       /* If we should pop to another file at EOF, do it. */
  281.       if (next_saved_file)
  282.     {
  283.       *bufp = input_scrub_pop (next_saved_file);    /* Pop state */
  284.       /* partial_where is now correct to return, since we popped it. */
  285.     }
  286.     }
  287.   return (partial_where);
  288. }                /* input_scrub_next_buffer() */
  289.  
  290. /*
  291.  * The remaining part of this file deals with line numbers, error
  292.  * messages and so on.
  293.  */
  294.  
  295.  
  296. int
  297. seen_at_least_1_file ()        /* TRUE if we opened any file. */
  298. {
  299.   return (physical_input_file != NULL);
  300. }
  301.  
  302. void
  303. bump_line_counters ()
  304. {
  305.   ++physical_input_line;
  306.   if (logical_input_line >= 0)
  307.     ++logical_input_line;
  308. }
  309.  
  310. /*
  311.  *            new_logical_line()
  312.  *
  313.  * Tells us what the new logical line number and file are.
  314.  * If the line_number is -1, we don't change the current logical line
  315.  * number.  If it is -2, we decrement the logical line number (this is
  316.  * to support the .appfile pseudo-op inserted into the stream by
  317.  * do_scrub_next_char).
  318.  * If the fname is NULL, we don't change the current logical file name.
  319.  */
  320. void 
  321. new_logical_line (fname, line_number)
  322.      char *fname;        /* DON'T destroy it! We point to it! */
  323.      int line_number;
  324. {
  325.   if (fname)
  326.     {
  327.       logical_input_file = fname;
  328.     }                /* if we have a file name */
  329.  
  330.   if (line_number >= 0)
  331.     logical_input_line = line_number;
  332.   else if (line_number == -2 && logical_input_line > 0)
  333.     --logical_input_line;
  334. }                /* new_logical_line() */
  335.  
  336. /*
  337.  *            a s _ w h e r e ()
  338.  *
  339.  * Return the current file name and line number.
  340.  * namep should be char * const *, but there are compilers which screw
  341.  * up declarations like that, and it's easier to avoid it.
  342.  */
  343. void 
  344. as_where (namep, linep)
  345.      char **namep;
  346.      unsigned int *linep;
  347. {
  348.   if (logical_input_file != NULL
  349.       && (linep == NULL || logical_input_line >= 0))
  350.     {
  351.       *namep = logical_input_file;
  352.       if (linep != NULL)
  353.     *linep = logical_input_line;
  354.     }
  355.   else if (physical_input_file != NULL)
  356.     {
  357.       *namep = physical_input_file;
  358.       if (linep != NULL)
  359.     *linep = physical_input_line;
  360.     }
  361.   else
  362.     {
  363.       *namep = (char *) "*unknown*";
  364.       if (linep != NULL)
  365.     *linep = 0;
  366.     }
  367. }                /* as_where() */
  368.  
  369.  
  370.  
  371.  
  372. /*
  373.  *            a s _ h o w m u c h ()
  374.  *
  375.  * Output to given stream how much of line we have scanned so far.
  376.  * Assumes we have scanned up to and including input_line_pointer.
  377.  * No free '\n' at end of line.
  378.  */
  379. void
  380. as_howmuch (stream)
  381.      FILE *stream;        /* Opened for write please. */
  382. {
  383.   register char *p;        /* Scan input line. */
  384.   /* register char c; JF unused */
  385.  
  386.   for (p = input_line_pointer - 1; *p != '\n'; --p)
  387.     {
  388.     }
  389.   ++p;                /* p->1st char of line. */
  390.   for (; p <= input_line_pointer; p++)
  391.     {
  392.       /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */
  393.       /* c = *p & 0xFF; JF unused */
  394.       as_1_char ((unsigned char) *p, stream);
  395.     }
  396. }
  397.  
  398. static void 
  399. as_1_char (c, stream)
  400.      unsigned int c;
  401.      FILE *stream;
  402. {
  403.   if (c > 127)
  404.     {
  405.       (void) putc ('%', stream);
  406.       c -= 128;
  407.     }
  408.   if (c < 32)
  409.     {
  410.       (void) putc ('^', stream);
  411.       c += '@';
  412.     }
  413.   (void) putc (c, stream);
  414. }
  415.  
  416. /* end of input_scrub.c */
  417.